package com.hevs.adibilis;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;

import org.json.JSONArray;
import org.json.JSONException;

import android.location.Location;
import android.location.LocationListener;
import android.os.Bundle;
import android.os.Message;

public class JSONFactory {

	// activities JSONArray
	private static JSONArray nodes = null;
	private static String url = "";

	public static final String TAG_ID = "id";
	public static final String TAG_NAME = "name";
	public static final String TAG_REFERENCE = "reference";
	public static final String TAG_DESCRIPTION = "description";
	public static final String TAG_ACCESS = "access";
	public static final String TAG_PARKING = "parking";
	public static final String TAG_WC = "wc";
	public static final String TAG_ADDRESS = "address";
	public static final String TAG_TELEPHONE = "telephone";
	public static final String TAG_LATITUDE = "latitude";
	public static final String TAG_LONGITUDE = "longitude";
	public static final String TAG_NPA = "npa";
	public static final String TAG_LOCALITY = "locality";
	public static final String TAG_IMAGE = "url";
	public static final String TAG_IMAGE_ID = "image_id";
	
	//gabioud - 12.03.2013
	public static final String TAG_EMAIL = "email";

	//---------------
	
	public static void getActivitiesForCategory(String idString) throws IOException, JSONException{

		clean();
		url = "http://adibilis.businessinformationsystem.ch/getActivitesForCategory.php?id=" + idString;
		nodes = new JSONArray(WebServiceAccess.downloadUrl(url));
	}

	public static ArrayList<HashMap<String, String>> parseActivitiesForCategory() throws JSONException, NoNodesException{

		if(nodes == null)
			throw new NoNodesException();

		// parsing JSON by field
		String[] fields = {TAG_ID, TAG_REFERENCE, TAG_NAME};
		return JSONParser.parseFromJSON(nodes, fields);

	}
	
	//---------------
	
	public static void getCategories() throws IOException, JSONException{

		clean();
		url = "http://adibilis.businessinformationsystem.ch/getCategories.php";
		nodes = new JSONArray(WebServiceAccess.downloadUrl(url));
	}
	
	

	public static ArrayList<HashMap<String, String>> parseCategories() throws JSONException, NoNodesException{

		if(nodes == null)
			throw new NoNodesException();

		// parsing JSON by field
		String[] fields = {TAG_ID, TAG_NAME};
		return JSONParser.parseFromJSON(nodes, fields);

	}

	//----------------
	//gabioud - 24.03.2013
		public static void getActivitiesSearch(String searchString) throws IOException, JSONException{

			clean();
			url = "http://adibilis.businessinformationsystem.ch/getActivitiesSearch.php?name="+searchString;
			nodes = new JSONArray(WebServiceAccess.downloadUrl(url));
		}
		
		
		public static ArrayList<HashMap<String, String>> parseActivitiesSearch() throws JSONException, NoNodesException{

			if(nodes == null)
				throw new NoNodesException();

			// parsing JSON by field
			String[] fields = {TAG_ID, TAG_REFERENCE, TAG_NAME};
			return JSONParser.parseFromJSON(nodes, fields);

		}
	
		
		
		
		
	
	//---------------
		
		//----------------
		//gabioud - 08.04.2013
			public static void getClosestActivities() throws IOException, JSONException{

				clean();
				url = "http://adibilis.businessinformationsystem.ch/getClosestActivities.php";
				nodes = new JSONArray(WebServiceAccess.downloadUrl(url));
			}
			
			
			public static ArrayList<HashMap<String, String>> parseClosestActivities() throws JSONException, NoNodesException{

				if(nodes == null)
					throw new NoNodesException();

				// parsing JSON by field
				String[] fields = {TAG_ID, TAG_REFERENCE, TAG_NAME, TAG_LATITUDE, TAG_LONGITUDE};
				return JSONParser.parseFromJSON(nodes, fields);

			}
		
			
			
			
			
		
		//---------------
	
	public static void getDetailActivity(String idString) throws IOException, JSONException{

		clean();
		url = "http://adibilis.businessinformationsystem.ch/getActivity.php?id=" + idString;
		nodes = new JSONArray(WebServiceAccess.downloadUrl(url));
	}

	public static ArrayList<HashMap<String, String>> parseDetailActivity() throws JSONException, NoNodesException{

		if(nodes == null)
			throw new NoNodesException();

		// parsing JSON by field
		String[] fields = {TAG_ID, TAG_NAME, TAG_REFERENCE, TAG_ACCESS, TAG_ADDRESS, TAG_DESCRIPTION, TAG_IMAGE,
				TAG_IMAGE_ID, TAG_LATITUDE, TAG_LONGITUDE, TAG_NPA, TAG_PARKING, TAG_TELEPHONE , TAG_WC, TAG_EMAIL};
		return JSONParser.parseFromJSON(nodes, fields);

	}

	//---------------
	
	
	public static void clean(){
		
		nodes = null;
		url = "";
	}


}
